package com.haorui.lygyd.controller;

import com.haorui.base.core.utils.BeanUtil;
import com.haorui.base.core.vo.PageableVo;
import com.haorui.lygyd.employeemanager.dto.SchoolDto;
import com.haorui.lygyd.employeemanager.dto.YewuleixingDto;
import com.haorui.lygyd.employeemanager.dto.YijianfankuiDto;
import com.haorui.lygyd.employeemanager.dto.YuangongdingdanDto;
import com.haorui.lygyd.employeemanager.entity.SchoolEntity;
import com.haorui.lygyd.employeemanager.entity.YewuleixingEntity;
import com.haorui.lygyd.employeemanager.entity.YijianfankuiEntity;
import com.haorui.lygyd.employeemanager.entity.YuangongdingdanEntity;
import com.haorui.lygyd.employeemanager.enumerate.JiesuanState;
import com.haorui.lygyd.employeemanager.service.SchoolService;
import com.haorui.lygyd.employeemanager.service.YewuleixingService;
import com.haorui.lygyd.employeemanager.service.YijianfankuiService;
import com.haorui.lygyd.employeemanager.service.YuangongdingdanService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 创建人: 张家魁
 * 日期: 2017/8/10
 * 时间: 16:16
 * 功能：
 */
@RestController
public class WxBusiController {

    @Autowired
    private SchoolService schoolService;
    @Autowired
    private YewuleixingService yewuleixingService;
    @Autowired
    private YuangongdingdanService yuangongdingdanService;
    @Autowired
    private YijianfankuiService yijianfankuiService;

    @RequestMapping(value = "/wxbusi/schools", method = RequestMethod.GET)
    public List<SchoolDto> schools() {
        List<SchoolEntity> entities = this.schoolService.findAll();
        List<SchoolDto> dtos = new ArrayList<>();
        for (SchoolEntity entity : entities) {
            SchoolDto dto = new SchoolDto();
            BeanUtil.beanCopy(entity, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @RequestMapping(value = "/wxbusi/busies", method = RequestMethod.GET)
    public List<YewuleixingDto> busies() {
        List<YewuleixingEntity> entities = this.yewuleixingService.findAll();
        List<YewuleixingDto> dtos = new ArrayList<>();
        for (YewuleixingEntity entity : entities) {
            YewuleixingDto dto = new YewuleixingDto();
            BeanUtil.beanCopy(entity, dto);
            dtos.add(dto);
        }
        return dtos;
    }

    @RequestMapping(value = "/wxbusi/order", method = RequestMethod.POST)
    public YuangongdingdanEntity order(@RequestBody YuangongdingdanDto dto) {
        YuangongdingdanEntity entity = new YuangongdingdanEntity();
        BeanUtil.beanCopy(dto, entity);
        entity.setYewuDate(new Date());
        entity.setJiesuanState(JiesuanState.WEI_JIESUAN);
        return yuangongdingdanService.save(entity);
    }

    @RequestMapping(value = "/wxbusi/order/{id}", method = RequestMethod.GET)
    public YuangongdingdanDto order(@PathVariable String id) {
        YuangongdingdanDto dto = new YuangongdingdanDto();
        YuangongdingdanEntity entity = yuangongdingdanService.findOne(id);
        BeanUtil.beanCopy(entity, dto);
        return dto;
    }

    @ApiOperation(value = "分页查询(可带查询条件)", notes = "返回实体列表", position = 2)
    @RequestMapping(value = "/wxbusi/orders", method = RequestMethod.POST)
    public Page<YuangongdingdanEntity> orders(@RequestBody PageableVo pageableVo) {
        return yuangongdingdanService.findAll(pageableVo.getPageModel().pageRequest(), pageableVo.getSearchConditions());
    }

    @ApiOperation(value = "查询该员工的所有订单", notes = "返回实体列表", position = 2)
    @RequestMapping(value = "/wxbusi/orders/{yuanGongId}", method = RequestMethod.GET)
    public List<YuangongdingdanEntity> statistics(@PathVariable String yuanGongId) {
        return yuangongdingdanService.findbyYuangongId(yuanGongId);
    }

    @ApiOperation(value = "问题反馈", notes = "返回实体", position = 2)
    @RequestMapping(value = "/wxbusi/feedback", method = RequestMethod.POST)
    public YijianfankuiEntity feedback(@RequestBody YijianfankuiDto dto) {
        YijianfankuiEntity entity = new YijianfankuiEntity();
        BeanUtil.beanCopy(dto, entity);
        entity.setYijianDate(new Date());
        return yijianfankuiService.save(entity);
    }
}
